<template>
	<div class="page">
		<div class="enterpriseInfoTab">
			<ul>
				<li>
					<a @click="$router.push('/dlyrl/oa/buspaycontract/BuspayContractList?id='+$route.query.id+'&types=0')
            ">
						付款
					</a>
				</li>
				<li class="defaultActive">
					<a
						@click="$router.push('/dlyrl/oa/commercialcontract/CommercialContractList?id='+$route.query.id+'&types=0')">
						收款
					</a>
				</li>
			</ul>
		</div>
		<el-form size="small" :inline="true" class="query-form" ref="searchForm" :model="searchForm"
			@keyup.enter.native="refreshList()" @submit.native.prevent>
			<!-- 搜索框-->
			<el-form-item prop="peopleDepartment.id">
				<SelectTree ref="peopleDepartment.id" :props="{
                      value: 'id',             // ID字段名
                      label: 'name',         // 显示名称
                      children: 'children'    // 子级字段名
                    }" size="small" placeholder="请选择所属部门" :url="`/sys/office/treeData?type=2`"
					:value="searchForm.peopleDepartment.id" :clearable="true" :accordion="true"
					@getValue="(value) => {searchForm.peopleDepartment.id=value}" />
			</el-form-item>
			<el-form-item prop="contractNumber">
				<el-input size="small" v-model="searchForm.contractNumber" placeholder="合同编号" clearable></el-input>
			</el-form-item>
			<el-form-item prop="contractName">
				<el-input size="small" v-model="searchForm.contractName" placeholder="合同名称" clearable></el-input>
			</el-form-item>
			<el-form-item prop="partyA">
				<el-input size="small" v-model="searchForm.partyA" placeholder="甲方" clearable></el-input>
			</el-form-item>
			<el-form-item prop="year">
				<el-date-picker v-model="searchForm.year" type="year" value-format="yyyy" format="yyyy"
					style="width: 100%;" @change="timeChange" :clearable="false" placeholder="签约时间/年">
				</el-date-picker>
			</el-form-item>
			<el-form-item prop="month">
				<el-date-picker v-model="searchForm.month" type="month" value-format="MM" format="MM"
					@change="timeChange" style="width: 100%;" :clearable="false" placeholder="签约时间/月">
				</el-date-picker>
			</el-form-item>
			<el-form-item style="float: right">
				<el-button type="primary" @click="refreshList()" size="small">查询</el-button>
				<el-button @click="resetSearch()" size="small">重置</el-button>
			</el-form-item>
		</el-form>
		<!-- 导入导出-->
		<el-dialog title="导入Excel" :visible.sync="isImportCollapse">
			<el-form :inline="true" v-show="isImportCollapse" class="query-form" ref="importForm">
				<el-form-item>
					<el-button type="default" @click="downloadTpl()" size="small">下载模板</el-button>
				</el-form-item>
				<el-form-item prop="loginName">
					<el-upload class="upload-demo"
						:action="`${this.$http.BASE_URL}/dlyrl/oa/commercialcontract/commercialContract/import`"
						:on-success="uploadSuccess" :show-file-list="true">
						<el-button size="small" type="primary">点击上传</el-button>
						<div slot="tip" class="el-upload__tip">只允许导入“xls”或“xlsx”格式文件！</div>
					</el-upload>
				</el-form-item>
			</el-form>
		</el-dialog>
		<div class="bg-white top">
			<el-row>
				<el-tabs v-model="activeName" @tab-click="handleClick" v-if="this.$route.query.types==0">
					<el-tab-pane label="实施中" name="0"></el-tab-pane>
					<el-tab-pane label="已完成" name="1"></el-tab-pane>
					<el-tab-pane label="未实施" name="2"></el-tab-pane>
				</el-tabs>
				<!-- <el-button v-if="hasPermission('dlyrl:oa:commercialcontract:commercialContract:add')" type="primary" size="small" icon="el-icon-plus" @click="add()">新建</el-button> -->
				<!-- <el-button v-if="hasPermission('dlyrl:oa:commercialcontract:commercialContract:edit')" type="warning" size="small" icon="el-icon-edit-outline" @click="edit()"
         :disabled="dataListSelections.length != 1" plain>修改</el-button> -->
				<!-- <el-button v-if="hasPermission('dlyrl:oa:commercialcontract:commercialContract:del')" type="danger"   size="small" icon="el-icon-delete" @click="del()"
                  :disabled="dataListSelections.length <= 0" plain>删除
        </el-button> -->
				<!-- <el-button-group class="pull-right">
            <el-button v-if="hasPermission('dlyrl:oa:commercialcontract:commercialContract:import')" type="default" size="small" icon="el-icon-upload2" title="导入" @click="isImportCollapse = !isImportCollapse, isSearchCollapse=false"></el-button>
            <el-button v-if="hasPermission('dlyrl:oa:commercialcontract:commercialContract:export')" type="default" size="small" icon="el-icon-download" title="导出" @click="exportExcel()"></el-button>
            <el-button
              type="default"
              size="small"
              icon="el-icon-refresh"
              @click="refreshList">
            </el-button>
        </el-button-group> -->
			</el-row>
			<el-table :data="dataList" :cell-style="cellStyles" :row-class-name="tableRowClassName"
				:header-cell-class-name="cellHeadStyle" style="width:100%; border:1px solid #d6cfe2;"
				@selection-change="selectionChangeHandle" @sort-change="sortChangeHandle" v-loading="loading"
				size="small" height="calc(100% - 80px)" @expand-change="detail" :summary-method="getSummaries"
				show-summary class="table">
				<!-- <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column> -->
				<!-- <el-table-column type="expand">
      <template slot-scope="scope">
      <el-tabs>
            <el-tab-pane label="回款信息">
                  <el-table
                  size="small"
                  :data="scope.row.paymentInfoList"
                  :cell-style="cellStyle"
                  :row-class-name="tableRowClassName"
                  :header-cell-class-name="cellHeadStyle"
                  style="width:100%; border:1px solid #d6cfe2;">
                <el-table-column
                    prop="date"
                    align="center"
                    show-overflow-tooltip
                    label="日期">
                  </el-table-column>
                <el-table-column
                    prop="money"
                    align="center"
                    show-overflow-tooltip
                    label="金额">
                  </el-table-column>
                <el-table-column
                    prop="capitalizedAmount"
                    align="center"
                    show-overflow-tooltip
                    label="大写金额">
                  </el-table-column>
                  <el-table-column
                    prop="confirmor.name"
                    align="center"
                    label="确认人">
                  </el-table-column>
                <el-table-column
                    prop="remarks"
                    align="center"
                    show-overflow-tooltip
                    label="备注信息">
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            </el-tabs>
      </template>
      </el-table-column> -->
				<el-table-column prop="contractNumber" show-overflow-tooltip align="center" fixed="left" label="合同编号">
					<!-- <template slot-scope="scope">
              <el-link  type="primary" :underline="false" v-if="hasPermission('dlyrl:oa:commercialcontract:commercialContract:edit')" @click="edit(scope.row.id)">{{scope.row.contractNumber}}</el-link>
              <el-link  type="primary" :underline="false" v-else-if="hasPermission('dlyrl:oa:commercialcontract:commercialContract:view')"  @click="view(scope.row.id)">{{scope.row.contractNumber}}</el-link>
              <span v-else>{{scope.row.contractNumber}}</span>
            </template> -->
					<template slot-scope="scope">
						<el-link type="primary" :underline="false" @click="view(scope.row.id)">
							{{scope.row.contractNumber}}</el-link>
					</template>
				</el-table-column>
				<el-table-column prop="contractName" show-overflow-tooltip align="center" label="合同名称">
				</el-table-column>
				<el-table-column prop="partyA" show-overflow-tooltip align="center" label="甲方">
				</el-table-column>
				<el-table-column prop="partyB.name" show-overflow-tooltip align="center" label="乙方">
				</el-table-column>
				<el-table-column prop="bPerson.office.name" show-overflow-tooltip align="center" width="100"
					label="归属部门">
				</el-table-column>
				<el-table-column prop="contractPerson.name" show-overflow-tooltip align="center" width="120"
					label="部门负责人">
				</el-table-column>
				<!-- <el-table-column
        prop="capitalizeContract"
        show-overflow-tooltip
        align="center"
        label="大写金额">
      </el-table-column>
    <el-table-column
        prop="capitalizeContract"
        show-overflow-tooltip
        align="center"
        label="大写金额">
      </el-table-column> -->
				<el-table-column prop="contractAmount" show-overflow-tooltip align="center" width="120"
					label="合同总金额(元)">
				</el-table-column>
				<!-- <el-table-column
        prop="capitalizeContract"
        show-overflow-tooltip
        align="center"
        label="大写金额">
      </el-table-column> -->
				<el-table-column prop="refundAmount" show-overflow-tooltip align="center" width="120" label="回款金额(元)">
				</el-table-column>
				<el-table-column prop="receiptRatio" show-overflow-tooltip align="center" width="120" label="回款比例(%)">
				</el-table-column>
				<el-table-column prop="unpaidAmount" show-overflow-tooltip align="center" width="120" label="未回款金额(元)">
				</el-table-column>
				<el-table-column prop="unpaidRatio" show-overflow-tooltip align="center" width="120" label="未回款比例(%)">
				</el-table-column>
				<el-table-column prop="earlyWarning" show-overflow-tooltip align="center" width="120" label="合同有效期">
				</el-table-column>
				<!-- <el-table-column
        prop="remarks"
        show-overflow-tooltip
        align="center"
        label="备注信息">
      </el-table-column> -->
				<el-table-column header-align="center" align="center" fixed="right" :key="Math.random()" width="70"
					label="操作">
					<template slot-scope="scope">
						<!-- <el-button v-if="hasPermission('dlyrl:oa:commercialcontract:commercialContract:view')" type="text" icon="el-icon-view" size="mini" @click="view(scope.row.id)">查看</el-button> -->
						<el-button v-if="hasPermission('dlyrl:oa:commercialcontract:commercialContract:edit')"
							type="text" icon="el-icon-edit" size="mini" @click="edit(scope.row.id)">修改</el-button>
						<!-- <el-button v-if="hasPermission('dlyrl:oa:commercialcontract:commercialContract:del')" type="text"  icon="el-icon-delete" size="small" @click="del(scope.row.id)">删除</el-button> -->
					</template>
				</el-table-column>
			</el-table>
			<el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageNo"
				:page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="total" background
				layout="total, sizes, prev, pager, next, jumper">
			</el-pagination>
		</div>
		<!-- 弹窗, 新增 / 修改 -->
		<el-dialog :title="title" size="small" width="70%" :visible.sync="visible">
			<CommercialContractForm ref="commercialContractForm" @refreshDataList="refreshList">
			</CommercialContractForm>
			<span slot="footer" class="dialog-footer">
				<el-button size="small" @click="closeDialog">关闭</el-button>
				<el-button size="small" type="primary" @click="save">确定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	import CommercialContractForm from './CommercialContractForm'
	import tableStyles from '../../../../../utils/mixins'
	import SelectTree from '@/components/treeSelect/treeSelect.vue'
	export default {
		mixins: [tableStyles],
		data() {
			return {
				activeName: '0', //默认进行中
				searchForm: {
					peopleDepartment: {
						id: ''
					},
					contractNumber: '',
					contractName: '',
					partyA: '',
					year: '',
					month: ''
				},
				dataList: [],
				pageNo: 1,
				pageSize: 10,
				total: 0,
				orderBy: '',
				dataListSelections: [],
				isImportCollapse: false,
				loading: false,
				visible: false,
				title: '', //标题
				times: '', //查询接口时间
				showSummary: false,
			}
		},
		components: {
			SelectTree,
			CommercialContractForm
		},
		activated() {
			this.refreshList()
		},
		watch: {
			//监听相同路由下参数变化的时候，从而实现异步刷新
			'$route'(to, from) {
				if (this.$route.query.types == '1') {
					this.activeName = '3';
					this.refreshList();
				} else if (this.$route.query.types == '0') {
					this.activeName = '0';
					this.refreshList();
				}
			}
		},
		methods: {
			handleClick() {
				this.$refs.searchForm.resetFields()
				this.refreshList()
			},
			//合同预警
			cellStyles({
				row,
				column,
				rowIndex,
				columnIndex
			}) {
				if (row.earlyWarning > 0 && row.earlyWarning < 10) {
					return "background-color:#CC3332;padding:0.15rem;border-left:1px solid #d6cfe2;";
				} else if (row.earlyWarning >= 10 && row.earlyWarning < 20) {
					return "background-color:#FF6601;padding:0.15rem;border-left:1px solid #d6cfe2;";
				} else if (row.earlyWarning >= 20 && row.earlyWarning < 30) {
					return "background-color:#FDFC09;padding:0.15rem;border-left:1px solid #d6cfe2;";
				} else if (row.earlyWarning == '已过期') {
					return "background-color:#C1C1C1;padding:0.15rem;border-left:1px solid #d6cfe2;";

				} else {
					return "padding:0.15rem;border-left:1px solid #d6cfe2;border-top:1px solid #d6cfe2";
				}
			},
			closeDialog() {
				this.visible = false;
				this.$refs.commercialContractForm.signatureVal = '';
			},
			//父组件调用子组件方法
			save() {
				this.$refs.commercialContractForm.submit();
				this.$refs.commercialContractForm.$refs['inputForm'].validate((valid) => {
					if (valid) {
						this.visible = false;
					}
				})
				setTimeout(this.refreshList, 1200)
			},
			timeChange() {
				console.log(this.searchForm.month, 'm')
				if (this.searchForm.year == '' && this.searchForm.month != '') {
					var date = new Date;
					var y = date.getFullYear()
					this.times = y + '-' + this.searchForm.month;
				} else if (this.searchForm.year != '' && this.searchForm.month == '') {
					this.times = this.searchForm.year;
				} else if (this.searchForm.year != '' && this.searchForm.month != '') {
					this.times = this.searchForm.year + '-' + this.searchForm.month
				} else if (this.searchForm.year == '' && this.searchForm.month == '') {
					this.times = ''
				}
			},
			// 获取数据列表
			refreshList() {
				console.log(this.activeName, '22')
				this.loading = true;
				this.$http({
					url: '/dlyrl/oa/commercialcontract/commercialContract/list',
					method: 'get',
					params: {
						'pageNo': this.pageNo,
						'pageSize': this.pageSize,
						'orderBy': this.orderBy,
						'peopleDepartment ': this.searchForm.peopleDepartment.id,
						'contractNumber': this.searchForm.contractNumber,
						'contractName': this.searchForm.contractName,
						'partyA': this.searchForm.partyA,
						'signingTime': this.times,
						'type': this.activeName,
						'peopleOffice': this.$route.query.id
					}
				}).then(({
					data
				}) => {
					if (data && data.success) {
						this.dataList = data.page.list
						this.total = data.page.count
						this.loading = false
					}
				})
			},
			// 每页数
			sizeChangeHandle(val) {
				this.pageSize = val
				this.pageNo = 1
				this.refreshList()
			},
			// 当前页
			currentChangeHandle(val) {
				this.pageNo = val
				this.refreshList()
			},
			// 多选
			selectionChangeHandle(val) {
				this.dataListSelections = val
			},

			// 排序
			sortChangeHandle(obj) {
				if (obj.order === 'ascending') {
					this.orderBy = obj.prop + ' asc'
				} else if (obj.order === 'descending') {
					this.orderBy = obj.prop + ' desc'
				} else {
					this.orderBy = ''
				}
				this.refreshList()
			},
			// 新增
			// add () {
			//   this.$refs.commercialContractForm.init(' ','add')
			// },
			// 修改
			edit(id) {
				this.visible = true;
				this.title = '合同详情'
				id = id || this.dataListSelections.map(item => {
					return item.id
				})[0]
				this.$nextTick(() => {
					this.$refs.commercialContractForm.init(id, 'edit')
				})
			},
			// 查看
			view(id) {
				this.visible = true;
				this.title = '合同详情'
				this.$nextTick(() => {
					this.$refs.commercialContractForm.init(id, 'view')
				})
			},
			// 删除
			// del (id) {
			//   let ids = id || this.dataListSelections.map(item => {
			//     return item.id
			//   }).join(',')
			//   this.$confirm(`确定删除所选项吗?`, '提示', {
			//     confirmButtonText: '确定',
			//     cancelButtonText: '取消',
			//     type: 'warning'
			//   }).then(() => {
			//     this.loading = true
			//     this.$http({
			//       url: '/dlyrl/oa/commercialcontract/commercialContract/delete',
			//       method: 'delete',
			//       params: {'ids': ids}
			//     }).then(({data}) => {
			//       if (data && data.success) {
			//         this.$message.success(data.msg)
			//         this.refreshList()
			//       }
			//       this.loading = false
			//     })
			//   })
			// },
			// 查看详情
			detail(row) {
				this.$http.get(`/dlyrl/oa/commercialcontract/commercialContract/queryById?id=${row.id}`).then(({
					data
				}) => {
					this.dataList.forEach((item, index) => {
						if (item.id === row.id) {
							item.paymentInfoList = data.commercialContract.paymentInfoList
						}
					})
				})
			},
			// 导入成功
			uploadSuccess(res, file) {
				if (res.success) {
					this.$message.success({
						dangerouslyUseHTMLString: true,
						message: res.msg
					})
				} else {
					this.$message.error(res.msg)
				}
			},
			// 下载模板
			downloadTpl() {
				this.$utils.download('/dlyrl/oa/commercialcontract/commercialContract/import/template')
			},
			exportExcel() {
				let params = {
					...this.searchForm
				}
				this.$utils.download('/dlyrl/oa/commercialcontract/commercialContract/export', params)
			},
			resetSearch() {
				this.$refs.searchForm.resetFields()
				this.refreshList()
			}
		}
	}
</script>
<style scoped>
	ul {
		margin: 0;
	}
</style>
